package com.springcloud.demo;

import com.alibaba.cloud.sentinel.annotation.SentinelRestTemplate;
import com.springcloud.demo.util.ExceptionUtil;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@EnableFeignClients
@SpringBootApplication
public class SentinelConsumerlApplication {
    public static void main(String[] args) {
        SpringApplication.run(SentinelConsumerlApplication.class, args);
    }

    @Bean(name = "restTemplate")
    @LoadBalanced  // RestTemplate 时添加 @LoadBalanced 负载均衡注解，表示这个 RestTemplate 在请求时拥有客户端负载均衡的能力。
    @SentinelRestTemplate(blockHandler = "handleException", blockHandlerClass = ExceptionUtil.class,
            fallback = "fallback", fallbackClass = ExceptionUtil.class)
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }
}
